<?php

namespace App\Jobs;

use App\Models\Reward;
use App\Models\WechatUser;
use Illuminate\Bus\Queueable;
use App\Business\BLL\Wechat\Official;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class SendRewardMsg implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $info;
    protected $balance;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Reward $reward, $balance)
    {
        $this->info = $reward;
        $this->balance = $balance;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $receive = WechatUser::find($this->info->receive);
        if ($receive && !empty($receive->wechat_openid)) {
            //发送通知
            $touser = $receive->wechat_openid;
            //发送
            $official = Official::make();
            $official->template_message->send([
                'touser' => $touser,
                'template_id' => 'dlF3VmQ19AgEfQhik_5ZhU8otEXdGEBhk9ew4rsXpcs',
                'miniprogram' => [
                    'appid' => config('wechat.mini_program.default.app_id'),
                    'pagepath' => 'pages/userInfo/rewardSuccess?id=' . $this->info->id,
                ],
                'data' => [
                    'first' => '你好，有球友给您投币了',
                    'keyword1' => round($this->info->money / 100, 2).'个',
                    'keyword2' => '投币',
                    'keyword3' => round($this->balance / 100, 2).'个',
                    'remark' => '点击查看打赏详情',
                ],
            ]);

        }
    }
}
